package com.jingdong.dao.impl;

import com.jingdong.dao.UserDao;
import com.jingdong.enity.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

    //这个是查询是否存在
    public boolean findUserByUsername(String username) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取连接
        String connection = "jdbc:mysql://localhost:3306/taobao";
        String dbUsername = "root";
        String dbPassword = "123456";
        Connection conn = DriverManager.getConnection(connection, dbUsername, dbPassword);
        //准备sql语句
        String sql = "select * from user where name =?";
        //获取传输器对象
        PreparedStatement pstm = conn.prepareStatement(sql);
        //给问号赋值
        pstm.setString(1, username);
        return pstm.executeQuery().next();

    }

    //查询所有用户
    public List<User> findAll() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取连接
        String connection = "jdbc:mysql://localhost:3306/taobao";
        String dbUsername = "root";
        String dbPassword = "123456";
        Connection conn = DriverManager.getConnection(connection, dbUsername, dbPassword);
        //准备sql语句
        String sql = "select * from user";
        //获取传输器对象
        PreparedStatement pstm = conn.prepareStatement(sql);
        ResultSet resultSet = pstm.executeQuery();
        ArrayList<User> userList = new ArrayList<>();
        while (resultSet.next()) {
            User user = new User(
                    resultSet.getInt("id"),
                    resultSet.getString("name"),
                    LocalDate.parse(resultSet.getString("birthday")));
            userList.add(user);


        }
        return userList;


    }
    //注册用户
    public int register(User user) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取连接
        String connection = "jdbc:mysql://localhost:3306/taobao";
        String dbUsername = "root";
        String dbPassword = "123456";
        Connection conn = DriverManager.getConnection(connection, dbUsername, dbPassword);
        //准备sql语句
        String sql = "insert into user(name,password,birthday) values(?,?,?)";
        //获取传输器对象
        PreparedStatement pstm = conn.prepareStatement(sql);
        //给问号赋值
        pstm.setString(1, user.getUsername());
        pstm.setString(2, user.getPassword());
        pstm.setString(3, user.getBirthday().toString());
        //执行sql语句

        return pstm.executeUpdate();


    }


    //登录的功能
    public boolean login(String username, String password) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取连接
        String connection = "jdbc:mysql://localhost:3306/taobao";
        String dbUsername = "root";
        String dbPassword = "123456";
        Connection conn = DriverManager.getConnection(connection, dbUsername, dbPassword);
        //准备sql语句
        String sql = "select * from user where name = ? and password = ?";
        //获取传输器对象
        PreparedStatement pstm = conn.prepareStatement(sql);
        //给问号赋值
        pstm.setString(1, username);
        pstm.setString(2, password);
        //执行sql语句
        return pstm.executeQuery().next();


    }
}
